%
% Author: Vasyl Mykhalchuk
%
function [ ] = female( dataFile, name )
% Modify matlab path
addpath(genpath('../util')); addpath(genpath('../io')); addpath(genpath('../strain'));
addpath(genpath('../export')); addpath(genpath('../obj-tools')); addpath(genpath('../preprocess'));
addpath(genpath('..')); addpath( genpath('../cloth') ); addpath( genpath ('../scalar-field') );
addpath( genpath('../neighborhood') );

resDir = 'res/';
% Load data
data = load( [resDir dataFile] );

[numberOfFrames, nV] = size(data.frames);
nV = nV / 3;
vertices = toMatrix(data.frames(1,:));
obj_ref.vertices = vertices;
obj_ref.faces = data.topology;
nF = size(data.topology, 1);

obj_ref.nF = nF;
obj_ref.nV = nV;

% default initialization to zeroes
sfMinGlobal = zeros(obj_ref.nV, 1);
sfMaxGlobal = zeros(obj_ref.nV, 1);
sfMeanGlobal = zeros(obj_ref.nV, 1);

% compute adjacency lists
% List of faces adjacent to each vertex

%{
LOG('Preprocessing');
adjList = createAdjacencyList(obj_ref.faces, obj_ref.nV, obj_ref.nF);
% List contains list of neighboring vertices for each vertex
adjVertList = createVertexAdjacencyList(obj_ref.faces, obj_ref.nV, obj_ref.nF);
save('elephant.mat', 'adjList', 'adjVertList');
%}

%TestAnimatedMesh( [resDir name] );

% Load Adj list
hmm = load( [resDir name 'Adj.mat'] );
adjList = hmm.adjList;
adjVertList = hmm.adjVertList;
% Load dist matrix
%{
distMatrix = load( [resDir name 'DistMatrix.mat'] );
distMatrix = distMatrix.distMatrix;
%}

% DEFORMATION FIELD
deformationField = zeros(obj_ref.nV, numberOfFrames);

% STEP THROUGH FRAMES
stepCount = 0;
numberOfFrames = 200;
for targetId = 2:numberOfFrames
    vertices = toMatrix(data.frames(targetId,:));
    obj_target.vertices = vertices;
    obj_target.faces = data.topology;
    %%
    sf = nextSF(obj_target, obj_ref, adjList, adjVertList);
    %%
    sfMeanGlobal = sfMeanGlobal + sf;
    stepCount = stepCount + 1;
    [sfMinGlobal, sfMaxGlobal] = calculateMinMax(sf, sfMinGlobal, sfMaxGlobal);
    deformationField(:, stepCount) = sf;
end%for

% CALCULATE ENTROPIES
hSF = zeros(obj_ref.nV, 1);
for i = 1:obj_ref.nV
    hSF(i) = entropy( deformationField(i, :), 4 );
end%for

% CALCULATE MEAN DURING THE ANIMATION
sfMeanGlobal = sfMeanGlobal / stepCount;

% SAVE RESULTS
outDir = 'target/';

LOG('Exporting... sfMax');
indeces = (1:nV)';
field = sfMaxGlobal;
field(:,2) = indeces;
field = sortrows(field,1);
colorList = flipud( rainbow( nV ) );
colorList(:,4) = field(:,2);
colorList = sortrows(colorList, 4);

%vertexColorList = createColorInfo(allVertices, sfMax, h);
path = getExportFileName(outDir, name, exportFileType.kSFMax);
exportPLY( path, obj_ref.vertices, colorList, obj_ref.faces);
%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
LOG('Exporting... sfMin');
indeces = (1:nV)';
field = sfMinGlobal;
field(:,2) = indeces;
field = sortrows(field,1);
colorList = flipud( rainbow( nV ) );
colorList(:,4) = field(:,2);
colorList = sortrows(colorList, 4);

%vertexColorList = createColorInfo(allVertices, sfMax, h);
path = getExportFileName(outDir, name, exportFileType.kSFMin);
exportPLY( path, obj_ref.vertices, colorList, obj_ref.faces);

%+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
LOG('Exporting... sfMean');
indeces = (1:nV)';
field = sfMeanGlobal;
field(:,2) = indeces;
field = sortrows(field,1);
colorList = flipud( rainbow( nV ) );
colorList(:,4) = field(:,2);
colorList = sortrows(colorList, 4);

%vertexColorList = createColorInfo(allVertices, sfMax, h);
path = getExportFileName(outDir, name, exportFileType.kSFMean);
exportPLY( path, obj_ref.vertices, colorList, obj_ref.faces);
%+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

%{
saveResultsOfTemporalIntegration(outDir, name, sfMinGlobal, sfMaxGlobal, sfMeanGlobal, hSF, obj_ref, adjVertList);
%}

end % function 